package com.zkt.subject.domain.handler.subject;

import com.zkt.subject.common.enums.SubjectInfoTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2024/10/6
 * @description 题目类型工厂
 **/

@Component
public class SubjectTypeHandlerFactory implements InitializingBean {
	
	@Resource
	private List<SubjectTypeHandler> subjectTypeHandlerList;
	
	private Map<SubjectInfoTypeEnum, SubjectTypeHandler> subjectTypeHandlerMap = new HashMap<>();
	
	public SubjectTypeHandler getHandler(int subjectType) {
		SubjectInfoTypeEnum subjectInfoTypeEnum = SubjectInfoTypeEnum.getByCode(subjectType);
		return subjectTypeHandlerMap.get(subjectInfoTypeEnum);
	}
	
	
	@Override
	public void afterPropertiesSet() throws Exception {
		subjectTypeHandlerList.forEach(subjectTypeHandler -> {
			subjectTypeHandlerMap.put(subjectTypeHandler.getHandlerType(), subjectTypeHandler);
		});
	}
}